package com.fpt.quanlyhethongsan.controller;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.fpt.quanlyhethongsan.utils.Constants;
import com.fpt.quanlyhethongsan.utils.RandomStringGenerator;
import com.fpt.quanlyhethongsan.utils.RandomStringGenerator.Mode;
import com.fpt.quanlysanbong.dao.SoccerOrderDao;
import com.fpt.quanlysanbong.dao.TimeDivisionDao;
import com.fpt.quanlysanbong.domain.Account;
import com.fpt.quanlysanbong.domain.SoccerOrder;

public class SoccerOrderController {
	
	private int id;
	private String operation;
	private boolean isSuccess;
	private SoccerOrder soccerOrder;
	private List<SoccerOrder> listSoccerOrder;
	private ArrayList<String> checkMonth;
	private ArrayList<String> checkDay;
	private ArrayList<Float> deposits;
	private ArrayList<String> depositsTemp;

	
	public ArrayList<Float> getDeposits() {
		return deposits;
	}

	public void setDeposits(ArrayList<Float> deposits) {
		this.deposits = deposits;
	}

	public ArrayList<String> getDepositsTemp() {
		return depositsTemp;
	}

	public void setDepositsTemp(ArrayList<String> depositsTemp) {
		this.depositsTemp = depositsTemp;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ArrayList<String> getCheckMonth() {
		return checkMonth;
	}

	public void setCheckMonth(ArrayList<String> checkMonth) {
		this.checkMonth = checkMonth;
	}

	public ArrayList<String> getCheckDay() {
		return checkDay;
	}

	public void setCheckDay(ArrayList<String> checkDay) {
		this.checkDay = checkDay;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public boolean isSuccess() {
		return isSuccess;
	}

	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}

	public SoccerOrder getSoccerOrder() {
		return soccerOrder;
	}

	public void setSoccerOrder(SoccerOrder soccerOrder) {
		this.soccerOrder = soccerOrder;
	}

	public List<SoccerOrder> getListSoccerOrder() {
		return listSoccerOrder;
	}

	public void setListSoccerOrder(List<SoccerOrder> listSoccerOrder) {
		this.listSoccerOrder = listSoccerOrder;
	}

	public String saveSoccerOrder() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		Account acc = (Account) session.getAttribute(Constants.USER_SESSION);
		listSoccerOrder = new ArrayList<SoccerOrder>();
		try{
			if (checkMonth != null)
				for (int i = 0; i < checkMonth.size(); i++) {
					SoccerOrder soccerOrder = new SoccerOrder();
					soccerOrder.setPerson(acc.getPerson());
					soccerOrder.setStartDate(new Date());
					Calendar cal = Calendar.getInstance();
					cal.add(Calendar.MONTH, 1);
					Date date = cal.getTime();
					soccerOrder.setEndDate(date);
					soccerOrder.setPerson(acc.getPerson());
					soccerOrder.setOrderNo("OD-"+RandomStringGenerator.generateRandomString(3, Mode.NUMERIC));
					soccerOrder.setTimeDivision(new TimeDivisionDao().getTimeDivisionById(Integer.parseInt(checkMonth.get(i))));
					float deposit = 0;
					try {
						deposit = deposits.get(getIndex(checkMonth.get(i)));
					}catch(Exception e){
						deposit = 0;
					}finally {
						soccerOrder.setDeposits(deposit);
					}
					listSoccerOrder.add(soccerOrder);
				}
			if (checkDay != null)
				for (int i = 0; i < checkDay.size(); i++) {
					SoccerOrder soccerOrder = new SoccerOrder();
					Date date = new Date();
					soccerOrder.setPerson(acc.getPerson());
					soccerOrder.setStartDate(date);
					soccerOrder.setEndDate(date);
					soccerOrder.setPerson(acc.getPerson());
					soccerOrder.setOrderNo("OD-"+RandomStringGenerator.generateRandomString(3, Mode.NUMERIC));
					soccerOrder.setTimeDivision(new TimeDivisionDao().getTimeDivisionById(Integer.parseInt(checkDay.get(i))));
					float deposit = 0;
					try {
						deposit = deposits.get(getIndex(checkDay.get(i)));
					}catch(Exception e){
						deposit = 0;
					}finally {
						soccerOrder.setDeposits(deposit);
					}
					listSoccerOrder.add(soccerOrder);
				}
			isSuccess = new SoccerOrderDao().addSoccerOrder(listSoccerOrder);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		if (isSuccess)
			return Constants.SUCCESS;
		else
			return Constants.ERROR;
	}
	
	public String getSoccerOrders() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		setOperation(Constants.UPDATE);
		Account acc = (Account) session.getAttribute(Constants.USER_SESSION);
		listSoccerOrder = new SoccerOrderDao().getListSoccerOrder(acc);
		return Constants.SUCCESS;
	}
	
	public String viewSoccerOrder() {
		return Constants.SUCCESS;
	}
	
	public String getFormatDate(Date date) {
		SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
		return f.format(date.getTime());
	}
	
	public String getPriceFormat(float number) {
		DecimalFormat f = new DecimalFormat("#,###,###");
		return f.format(number);
	}
	
	public String deleteSoccerOrder() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		isSuccess = new SoccerOrderDao().deleteSoccerOrder(id);
		if(isSuccess)
			return Constants.SUCCESS;
		else
			return Constants.ERROR;
	}
	
	public boolean checkResetOrder(Date endDate) {
		boolean check = false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date currentDate = new Date();
		if(sdf.format(endDate).compareTo(sdf.format(currentDate)) >= 0){
				check = true;
		}
		return check;
	}
	
	private int getIndex(String value) {
		int index = -1;
		for(int i = 0; i < depositsTemp.size(); i++) {
			if(depositsTemp.get(i).equalsIgnoreCase(value))
				return i;
		}
		return index;
	}
}
